<?php

namespace App\Http\Controllers\Admin;
use App\Models\Admin\Permission;
use App\Models\Admin\Role;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\Admin\Role\CreateRequest;
use App\Http\Requests\Admin\Role\UpdateRequest;
use Illuminate\Support\Facades\DB;
use Style;


class RoleController extends CommonController{
   public function __construct(){
       $this->middleware('checkPermission:Role|Role-index', ['only' => ['index']]);
       $this->middleware('checkPermission:Role|Role-create', ['only' => ['create', 'store']]);
       $this->middleware('checkPermission:Role|Role-edit', ['only' => ['edit', 'update']]);
       $this->middleware('checkPermission:Role|Role-destroy', ['only' => ['destroy']]);
       $this->middleware('checkPermission:Role|Role-permission', ['only' => ['permissions','storePermissions']]);
    }

    /**
     * 角色列表
     * @return mixed
     */
    public function index(){
        $roles = Role::paginate(10);
        return Style::view('admin.rbac.roles.index',compact('roles'));
    }

    /**
     * 添加角色
     * @return mixed
     */
    public function create(){
        return Style::view('admin.rbac.roles.create');
    }

    /**
     * 添加角色提交
     * @param CreateRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateRequest $request){
        $result = Role::create($request->all());
        if($result){
            flash('角色添加成功', 'success');
            return redirect('admin/role');
        }else{
            return back()->with('errors','角色添加失败，请稍后重试！');
        }
    }

    /**
     * 编辑角色
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $rolefield = Role::find($id);
        return Style::view('admin.rbac.roles.edit',compact('rolefield'));
    }

    /**
     * 更新角色
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateRequest $request,$id){
        $input = $request->except(['_method','_token']);
        $result = Role::where('id',$id)->update($input);
        if($result){
            flash('更新角色成功', 'success');
            return redirect('admin/role');
        }else{
            return back()->with('errors','角色信息更新失败，请稍后重试！');
        }
    }

    /**
     * 删除角色
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id){
        $result = Role::where('id',$id)->delete();
        if($result){
            DB::table('role_user')->where('role_id',$id)->delete();
            DB::table('permission_role')->where('role_id',$id)->delete();
            return response()->json(['status' => 0,'msg' => '角色删除成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '角色删除失败，请稍后重试！']);
        }
    }

    /**
     * 分配角色权限
     * @param string $id
     * @return mixed
     */
    public function permissions($id=''){
        $permission = Permission::orderBy('sort')->get()->toArray();
        //设置无限级等级处理
        $permissions = (new Permission())->cateLevel($permission);
        //当前角色所对应的权限
        $permission_role = DB::table('permission_role')->where('role_id',$id)->pluck('permission_id');

        //分配当前角色权限
        $permissionArr = (new Permission())->assignPermissions($permissions,$permission_role);
		//dd($permissionArr);
        return Style::view('admin.rbac.roles.permissions', compact('permissionArr','id'));
    }

    /**
     * 分配设置角色权限
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function storePermissions(Request $request){
        $input = $request->except('_token');
        DB::table('permission_role')->where('role_id',$request->input('role_id'))->delete();
        if(array_key_exists('permissions',$input)){
            $permissionsID = array_filter(array_unique($request->input('permissions')));
            $roles = Role::where('id','=',$request->input('role_id'))->first();
            $permissionsArr = Permission::whereIn('id', $permissionsID)->get();
            //关联权限和角色之间的中间表
            $roles->attachPermissions($permissionsArr);
        }
        flash('权限配置成功', 'success');
        return redirect('admin/role');
    }
}